package com.airwallex.strategy.impl;

import com.airwallex.util.constant.ProjectConstants;
import com.airwallex.model.ExpParam;
import com.airwallex.strategy.CalculateStrategy;
import com.airwallex.util.CalculatorUtils;
import com.airwallex.util.exception.InsufficientParamException;

import java.math.BigDecimal;
import java.util.List;
import java.util.Stack;

public class AddStrategy implements CalculateStrategy {

    @Override
    public void execCalc(Stack<ExpParam> numbers, Stack<List<ExpParam>> logs) {
        if (numbers.size() > 1) {
            BigDecimal opnd2 = numbers.pop().getNum();
            BigDecimal opnd1 = numbers.pop().getNum();
            BigDecimal result = opnd1.add(opnd2)
                    .setScale(ProjectConstants.RESULT_SCALE, ProjectConstants.ROUNDING_MODE);
            ExpParam resultParam = ExpParam.builder()
                    .content(result.toString())
                    .num(result)
                    .pos(-1)
                    .isNumber(true)
                    .build();
            numbers.push(resultParam);
            CalculatorUtils.addLog(numbers, logs);
        } else {
            throw new InsufficientParamException("insucient parameters !");
        }
    }
}
